package com.gm.wholesale;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Keep;
import androidx.multidex.MultiDex;

import com.gm.commonlib.util.logUtils.LogUtil;
import com.gm.commonlib.views.toast.ToastUtil;
import com.gm.wholesale.cacheData.PatchCache;
import com.gm.wholesale.constant.Constant;
import com.taobao.sophix.PatchStatus;
import com.taobao.sophix.SophixApplication;
import com.taobao.sophix.SophixEntry;
import com.taobao.sophix.SophixManager;
import com.taobao.sophix.listener.PatchLoadStatusListener;

public class SophixStubApplication extends SophixApplication {

    @Keep
    @SophixEntry(GMApplication.class)
    static class RealApplicationStub {}
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
//         如果需要使用MultiDex，需要在此处调用。
         MultiDex.install(this);
         initSophix();
    }
    private void initSophix() {
        String appVersion = "0.0.0";
        try {
            appVersion = this.getPackageManager()
                    .getPackageInfo(this.getPackageName(), 0)
                    .versionName;
        } catch (Exception e) {
        }
        final SophixManager instance = SophixManager.getInstance();
        instance.setContext(this)
                .setUsingEnhance(true) // 适配加固模式,如果app使用了加固则需要加上此方法
                .setAppVersion(appVersion)
                .setSecretMetaData(null, null, null)
                .setEnableDebug(true)
                .setEnableFullLog()
                .setPatchLoadStatusStub(new PatchLoadStatusListener() {
                    @Override
                    public void onLoad(final int mode, final int code, final String info, final int handlePatchVersion) {
                        Log.d("gm_app","热修复 "+code+": "+info);
                        if (code==PatchStatus.CODE_DOWNLOAD_SUCCESS){
                            Log.d("gm_app","热修复 补丁下载 成功");
                            setReStartApp();
                        }
                       else if (code == PatchStatus.CODE_LOAD_SUCCESS) {
                            Log.d("gm_app","热修复 加载 成功!");
                        } else if (code == PatchStatus.CODE_LOAD_RELAUNCH) {
                            Log.d("gm_app","热修复 安装 成功，等待重启.");
                        } else if (code == PatchStatus.CODE_REQ_CLEARPATCH) {
                            Log.d("gm_app","热修复 回滚 成功，等待重启.");
                        } else if (code == PatchStatus.CODE_REQ_NOUPDATE) {
                            Log.d("gm_app","热修复 没有 可更新补丁!");
                        }
                    }
                }).initialize();


    }
    private void setReStartApp(){
        Intent intent=new Intent();
        intent.setAction(Constant.reStartApp);
        sendBroadcast(intent);
    }
}
